from flask import Flask, render_template, Response, request, jsonify, stream_with_context
import RPi.GPIO as GPIO
import threading
#from components.motor_pmw import Motor
from components.motor_pigpio import Motor
import json

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

ENA=14
ENB=21

IN1=23
IN2=24

IN3=16
IN4=20

motor = Motor(IN1,IN2,IN3,IN4,ENA,ENB)


app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def main():
    return render_template("index.html")


@app.route('/car_action', methods=['GET'])
def car_action():
    m_action = int(request.args.get('action'))
    motor.action(m_action)
    return jsonify({'motor status':m_action})


@app.route('/add_min_speed', methods=['GET'])
def add_min_speed():
    add_min_speed = int(request.args.get('add_min_speed'))
    motor.adjust_speed(add_min_speed)
    return jsonify({'add_min_speed': add_min_speed})

@app.route('/turn_speed', methods=['GET'])
def turn_speed():
    turn_speed =int(request.args.get('turn_speed'))
    motor.adjust_turn_speed(turn_speed)
    return jsonify({'turn_speed': turn_speed})

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=False)
    GPIO.cleanup()
